package icu.sehnen.dao.impl;

import icu.sehnen.dao.UserDao;
import icu.sehnen.model.UserModel;
import icu.sehnen.utils.JDBCUtils;

import java.sql.*;

public class UserDaoImpl implements UserDao {
//    查找用户
    @Override
    public boolean findUser(UserModel userModel) {

        Connection conn = null;
        PreparedStatement pstmt =  null;
        ResultSet rs = null;

        String sql="select * from user where username= ? and password= ? ";
        try {
            conn= JDBCUtils.getConnection();
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,userModel.getUsername());
            pstmt.setString(2,userModel.getPassword());
            rs=pstmt.executeQuery();
            return  rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return  false;

    }
//    添加用户
    @Override
    public boolean addUser(UserModel userModel) {
        Connection conn = null;
        PreparedStatement pstmt =  null;
        int res;

        String sql="INSERT INTO `user` ( `username`, `password`, `phone`, `email`, `sex`," +
                " `hobby`, `birthday`) VALUES ( ?,?, ?, ?, ?, ?, ?)";

        try {
            conn=JDBCUtils.getConnection();
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,userModel.getUsername());
            pstmt.setString(2,userModel.getPassword());
            pstmt.setString(3,userModel.getPhone());
            pstmt.setString(4,userModel.getEmail());
            pstmt.setString(5,userModel.getSex());
            pstmt.setString(6,userModel.getHobby());
            pstmt.setString(7,userModel.getBirthday());
            res=pstmt.executeUpdate();
            if(res==1){
                return  true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }
        return  false;
    }
}
